#encoding:utf-8
import urllib
import pdb
import excelwrite
from excelwrite import *

class Goods(object):
	'The Googs info : price name'
	def __init__(self, name, price):
		self.name = name
		self.price = price

def geturldata(urlname):
	listinfo = []
	try:
		content = urllib.urlopen(urlname).read()
		
		#获取名字
		detail = content.find(r'class="detail');
		title  = content.find(r'<a  title=', detail )
		href = content.find(r'href=', title)
		
		#获取价格
		priceStart = content.find(r'<span>', href)
		priceEnd  = content.find(r'</span>', priceStart)
		
		#获取图片
		imgStart = content.find(r'<img src=', priceEnd)
		imgEnd = content.find(r'.jpg', imgStart)
		
		text = content[title + 11:href -2]
		price = content[priceStart + 6:priceEnd]
		img = content[imgStart+10:imgEnd+4]
		httpImg = 'http:' + img
		#ImgConntent = urllib.urlopen(httpImg).read()
		
		info = Goods(text,float(price))
		listinfo.append(info)
		
		while 1:
			detail = content.find(r'class="detail', priceEnd)
			if detail == -1:
				break
			title  = content.find(r'<a  title=', detail)

			href = content.find(r'href=', title)
			
			priceStart = content.find(r'<span>', href)
			priceEnd  = content.find(r'</span>', priceStart)	
			imgStart = content.find(r'<img src=', priceEnd)
			imgEnd = content.find(r'.jpg', imgStart)
			
			text = content[title + 11:href -2]
			price = content[priceStart + 6:priceEnd]
	
			img = content[imgStart+10:imgEnd+4]
			httpImg = 'http:' + img

			#ImgConntent = urllib.urlopen(httpImg).read()
			info = Goods(text,float(price))	
			listinfo.append(info)
	except:
		print "open failed"
	return listinfo

if __name__ == '__main__':
	ulrname = 'http://www.jd.com/pinpai/9735-59540.html'
	listinfo = geturldata(ulrname)
	writeexcel(listinfo)
	# for i in range(len(listinfo)):
		# print listinfo[i].name
		# print listinfo[i].price
	